package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Report;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.Shield;
import com.net.tkc.pro.impl.ShieldImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Shield/")
public class ShieldController {
    ResultEntity resultEntity = null;
    @Resource
    ShieldImpl shieldImpl;

    @PostMapping("Shield")
    private String addShield(
            @RequestParam("userfrom") int userfrom,
            @RequestParam("userto") int userto
    ){
        resultEntity = new ResultEntity();
        Shield shield=new Shield();
        shield.setUserfrom(userfrom);
        shield.setUserto(userto);
        try{
            int result = shieldImpl.addShield(shield);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 查看自己屏蔽了谁
     * @param userfrom
     * @return
     */
    @GetMapping("Shield")
    private String getUserToList(
            @RequestParam("userfrom") int userfrom
    ){
        resultEntity = new ResultEntity();
        try{
            List<Shield> list =shieldImpl.getUserToList(userfrom);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 屏蔽或者取消屏蔽
     * @param state 状态
     * @param id id
     * @return
     */
    @PutMapping("Shield")
    private String upShield(
            @RequestParam("state") int state,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = shieldImpl.upShield(state,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
